# Create your views here.

from census.forms import FamilyForm
from census.models import Family, AdultDependant, ChildDependant, KABILAH_LIST
from django.db.models import Count
#from django.db.models.query_utils import Q
from django.db.models import Q
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.template import loader
from django.template.context import RequestContext, Context
from django.template.loader import get_template
from itertools import chain
import sys

def reports(request):
    try:
        t = loader.get_template('reports/reports.html')
        c = RequestContext(request)
        return HttpResponse(t.render(c))
    except:
        raise
    
def kabilah_report(request):
    try:
        # Find Kabilah List
        kabilahs = []
        for k, x in KABILAH_LIST:
            kabilahs.append(str(k))
        
        total_count = [0, 0, 0, 0, 0]
        kabilah_detail = {}
        # Gather each kabilah details
        for kabilah in kabilahs:
            males = Family.objects.filter(caste=kabilah, sex = 'M').count()
            males += AdultDependant.objects.filter(guardian__caste=kabilah, sex = 'M').count()
            females = Family.objects.filter(caste=kabilah, sex = 'F').count()
            females += AdultDependant.objects.filter(guardian__caste=kabilah, sex = 'F').count()
            boys = ChildDependant.objects.filter(guardian__caste=kabilah, sex = 'M').count()
            girls = ChildDependant.objects.filter(guardian__caste=kabilah, sex = 'F').count()
            total = (males + females + boys + girls)
            kabilah_detail[kabilah] = [males, females, boys, girls, total]
            total_count[0] += males
            total_count[1] += females
            total_count[2] += boys
            total_count[3] += girls
            total_count[4] += total
        
        kabilah_detail_sorted = sorted(kabilah_detail.items())
        del kabilah_detail
            
        return render_to_response('reports/kabilah_report.html',
                                  {"kabilah_detail" : kabilah_detail_sorted,
                                    "total_count": total_count,
                                    },
                                  context_instance=RequestContext(request))
    except:
        raise

def city_kabilah_report(request):
    try:
        # Get list of cities
        cities = Family.objects.values_list('h_city', flat=True).distinct()
        # Get kabilah list
        kabilahs = []
        for k, x in KABILAH_LIST:
            kabilahs.append(str(k))
        #TODO: City count passed as kabilah name
        city_kabilah_count = 'zzzz'
        
        city_details = {}
        for city in cities:
            kabilah_details = {}
            city_total = 0
            for kabilah in kabilahs:
                kabilah_total = Family.objects.filter(h_city=city, caste=kabilah).count()
                kabilah_total += AdultDependant.objects.filter(guardian__h_city=city, guardian__caste=kabilah).count()
                kabilah_total += ChildDependant.objects.filter(guardian__h_city=city, guardian__caste=kabilah).count()
                kabilah_details[kabilah] = kabilah_total
                city_total += kabilah_total
            
            kabilah_details[city_kabilah_count] = city_total
            city_details[city] = sorted(kabilah_details.items())
            del kabilah_details
            
        city_details_sorted = sorted(city_details.items())
        del city_details
            
        return render_to_response('reports/city_kabilah_report.html',
                                  {"city_details": city_details_sorted},
                                  context_instance=RequestContext(request))
    except:
        raise
    
def social_worker_report(request):
    try:
        kabilahs = []
        for k, x in KABILAH_LIST:
            kabilahs.append(str(k))
        kabilah_workers = {}
        total_workers = 0
        for kabilah in kabilahs:
            count = Family.objects.filter(Q(caste=kabilah), 
                                          Q(member_hussainia=True) | Q(member_other=True)).count()
            kabilah_workers[kabilah] = count
            total_workers += count
        
        kabilah_workers_sorted = sorted(kabilah_workers.items())
        del kabilah_workers
        
        return render_to_response('reports/social_workers.html',
                                  {"kabilah_workers": kabilah_workers_sorted,
                                   "total_workers":total_workers},
                                  context_instance=RequestContext(request)) 
    except:
        raise


def kabilah_voters_report(request):
    try:
        kabilahs = []
        for k, x in KABILAH_LIST:
            kabilahs.append(str(k))
        kabilah_voters = {}
        total_voters = 0
        for kabilah in kabilahs:
            count = Family.objects.filter(caste=kabilah, registered_voter=True).count()
            kabilah_voters[kabilah] = count
            total_voters += count
        
        kabilah_voters_sorted = sorted(kabilah_voters.items())
        del kabilah_voters
        return render_to_response('reports/kabilah_voters.html',
                                  {'kabilah_voters':kabilah_voters_sorted,
                                   "total_voters":total_voters},
                                  context_instance=RequestContext(request))
    except:
        raise

def city_report(request):
    try:
        # Find unique cities
        cities = Family.objects.values_list('h_city', flat=True).distinct()
        city_details = {}
        total_count = [0, 0, 0, 0, 0]
        for city in cities:
            males = Family.objects.filter(h_city=city, sex = 'M').count()
            males += AdultDependant.objects.filter(guardian__h_city=city, sex = 'M').count()
            females = Family.objects.filter(h_city=city, sex = 'F').count()
            females += AdultDependant.objects.filter(guardian__h_city=city, sex = 'F').count()
            boys = ChildDependant.objects.filter(guardian__h_city=city, sex = 'M').count()
            girls = ChildDependant.objects.filter(guardian__h_city=city, sex = 'F').count()
            total = (males + females + boys + girls)
            city_details[city] = [males, females, boys, girls, total]
            total_count[0] += males
            total_count[1] += females
            total_count[2] += boys
            total_count[3] += girls
            total_count[4] += total
        city_details_sorted = sorted(city_details.items())
        del city_details
        
        return render_to_response('reports/city_report.html',
                                  {"city_details":city_details_sorted,
                                   "total_count":total_count,},
                                  context_instance=RequestContext(request))
    except:
        raise


                

    
